package view.impl;

import dao.FoodDao;
import dao.impl.FoodDaoImpl;
import model.Food;
import view.FoodView;

import java.math.BigDecimal;
import java.util.List;
import java.util.Scanner;

/**
 * @author cyc20
 */
public class FoodViewImpl implements FoodView {
    private FoodDao foodDao=new FoodDaoImpl();
    @Override
    public List<Food> showFoodList(Integer businessId) {
        List<Food> list=foodDao.listFoodbyBusinessId(businessId);
        return list;
    }

    @Override
    public void saveFood(Integer businessId) {
        Food food = new Food();
        food.setBusinessId(businessId);
        Scanner sc = new Scanner(System.in);
//        private String foodName;
//        private String foodExplain;
//        private BigDecimal foodPrice;

        System.out.println("请输入食物名称：");
        String foodName = sc.nextLine();

        System.out.println("请输入食物简介：");
        String foodExplain = sc.nextLine();

        System.out.println("请输入食物价格：");
        BigDecimal foodPrice = new BigDecimal(sc.nextLine());


        food.setFoodName(foodName);
        food.setFoodExplain(foodExplain);
        food.setFoodPrice(foodPrice);
        foodDao.saveFood(food);
        System.out.println("新建完成");
    }

    @Override
    public void updateFood(Integer businessId) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要修改的食物Id：");
        Integer foodId = Integer.parseInt(sc.nextLine());
        List<Food> list = foodDao.listFoodByBusinessId(businessId);
        boolean flag=true;
        if(list!=null&&list.size()>0){
            for (int i = 0; i < list.size(); i++) {
                if(list.get(i).getFoodId().equals(foodId)){
                    flag=false;
                }
            }
        }
        if(flag){
            System.out.println("您无权操作这个数据。");
            return ;
        }

        System.out.println("请输入要修改的食物名称：");
        String foodName = sc.nextLine();
        if (foodName == null || foodName.equals("")) {
            foodName = null;
        }

        System.out.println("请输入要修改的食物简介：");
        String foodExplain = sc.nextLine();
        if (foodExplain == null || foodExplain.equals("")) {
            foodExplain = null;
        }

        System.out.println("请输入要修改的食物价格：");
        String foodPriceStr = sc.nextLine();
        BigDecimal foodPrice = null;
        if (foodPriceStr == null || foodPriceStr.equals("")) {
            foodPrice = null;
        } else {
            foodPrice = new BigDecimal(foodPriceStr);
        }
        Food food = new Food(foodId, foodName, foodExplain, foodPrice, businessId);
        foodDao.updateFood(food);
        System.out.println("修改完成");
    }
    @Override
    public void removeFood(Integer businessId) {
        Scanner sc = new Scanner(System.in);

        System.out.println("请输入要删除的食物Id：");
        Integer foodId = Integer.parseInt(sc.nextLine());
        List<Food> list = foodDao.listFoodByBusinessId(businessId);
        boolean flag=true;
        for (int i = 0; i < list.size(); i++) {
            if(foodId.equals(list.get(i).getFoodId())){
                foodDao.removeFood(foodId);
                System.out.println("删除成功");
                flag=false;
            }
        }
        if(flag){
            System.out.println("您无权操作这个数据。");
        }
    }
}
